<%@ include file="init.jsp"%>

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.TimeZone"%>
<%@ page import="java.text.DateFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Locale"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="com.chalmerslosers.*"%>
<%@ page import="com.chalmerslosers.utils.*"%>
<% 
	DateFormat dateFormatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT ); //( "yyyy-MM-DD, hh:mm:ss" );
	dateFormatter.setTimeZone( TimeZone.getTimeZone("GMT+01:00") );

	Cookie[] cookies = renderRequest.getCookies();
	
	boolean flatMode = false;
	long lastVisit = 0;

	if ( cookies != null ){	
		for ( Cookie c : cookies ){
			if ( c.getName().equals("flatMode") ){
				if ( c.getValue().equals("true") ){
					flatMode = true;
				}
				else
				{				
					flatMode = false;
				}
			}
			else if ( c.getName().equals( "lastVisit" ) ){				
				try{
					lastVisit = Long.parseLong( c.getValue() );
				} catch ( NumberFormatException e )
				{
					lastVisit = new Date().getTime();
				}
			}
		}
	}

	long ticksStart = new Date().getTime();
	
	Date flatModeCookieExpiration = new Date();
	flatModeCookieExpiration.setTime( new Date().getTime() + 10*365*24*60*60*1000 );	
	
	

	
	

%>






<portlet:actionURL var="postUrl">
	<portlet:param name="action" value="post" />
</portlet:actionURL>

<portlet:renderURL var="reloadUrl"/>

<form:form action="${reloadUrl}" id="reloadForm">
</form:form>

<link rel="stylesheet" type="text/css" href="css/styles.css" />


<jsp:include page="./next-fragment.jsp"></jsp:include>
<a href="javascript:reply('', '')">Nytt inlägg</a>
<a href="javascript:setFlatMode(<%= flatMode ? "false" : "true" %>)"><%= flatMode ? "Visa trådar" : "Visa platt lista" %></a>
&nbsp;&nbsp;&nbsp;&nbsp;


<hr />

<%
	   // PersistenceManager pm = PMF.get().getPersistenceManager();
	List<Post> flatListPosts = (List<Post>)request.getAttribute("flatListPosts");

	List<Post> posts = (List<Post>)request.getAttribute("parentPosts");
	//List<Post> parentPosts = (List<Post>)request.getAttribute("parentPosts");
	List<Post> childrenPosts = (List<Post>)request.getAttribute("childPosts");  
		if ( flatMode )
		{
			//String query = "select from " + Post.class.getName() + " order by date desc";
		   
			
			
			HashMap<Long, String> names = new HashMap<Long,String>( posts.size() );
			
			for (Post p : flatListPosts) { 
				
				names.put( p.getId(), p.getAuthorNickName() );
			}
			
			for (Post p : flatListPosts) { 
				 
				String author = p.getAuthorNickName();
					
				String reply = "";
				
				if ( p.getParentId() != null && names.get(p.getParentId())!=null)
				{
					String parentName = names.get(p.getParentId());
					reply = parentName + ", ";					
				}
			
				// parent
	%>

<div class="toppost"><i><%= reply %></i> <%= p.getContent() %><br />

Av: <b><%= author %></b>, <i>id: <%= p.getId() %>, </i> <%= dateFormatter.format(p.getDate()) %>
<a href="javascript:reply(<%= p.getId() %>, <%= (p.getTopParentId()==-1)?p.getId():p.getTopParentId() %>)">Svara</a>

</div>

<%
			}
			
		}
		else
		{
	
			
			for (Post p : posts) {
				boolean colourToggle = false;
				String topAuthor = p.getAuthorNickName();

	        	%>
<div class="toppost"><%= p.getContent() %><br />
Av: <b><%= topAuthor %></b>, <i>id: <%= p.getId() %>, </i> <%= dateFormatter.format( p.getDate() ) %>
<a href="javascript:reply(<%= p.getId() %>, <%= p.getId() %>)">Svara</a>
</div>

<% 
	  				// Find children
		  			String childQuery = "select from " + Post.class.getName() + " where topParentId == " + p.getId() + " order by date";
	  				
	  				
					  
					
					ParentPost parent = new ParentPost( p, childrenPosts);
	  				
	  				for ( ParentPost.ChildPost c : parent.getChildren() ){
	  					 
	  		        	colourToggle = !colourToggle;
	  		        	String author = c.getPost().getAuthorNickName();	  		        	
	  		        	boolean isNewPost = lastVisit <= c.getPost().getDate().getTime();
	  		        	String colour = isNewPost ? "#ddffdd" : colourToggle ? "#dddddd" : "#eeeeee";
	  		        	String borderColour = isNewPost ? "#55cc55" : "black";
	  		        	
	  		        	String boxStyle = isNewPost ? "new" : colourToggle ? "zig" : "zag";
	  		        	
	  			%>

<div class="childmini_<%= boxStyle %>"
	id="mini_<%= c.getPost().getId() %>"
	onclick="showFull(<%= c.getPost().getId() %>)"
	style="	display:<%= isNewPost ? "none" : "block"%>; 
	  									margin-left: <%= c.getIndentation()*15 %>px">

<b><%= author %></b>: <%= c.getPost().getContent() %></div>

<div class="childfull_<%= boxStyle %>"
	id="full_<%= c.getPost().getId() %>"
	ondblclick="showMini(<%= c.getPost().getId() %>)"
	style="	display:<%= isNewPost ? "block" : "none"%>;  
	  									margin-left: <%= c.getIndentation()*15 %>px">

<%= c.getPost().getContent() %><br />

Av: <b><%= author %></b>, <font size="-2"><i>id: <%= c.getPost().getId() %>,
parent id: <%= c.getPost().getParentId() %></i></font> <%= dateFormatter.format( c.getPost().getDate() ) %>
<a href="javascript:reply(<%= c.getPost().getId() %>, <%= p.getId() %>)">Svara</a>
</div>
<%
	  				}  
	  			%>

<%
	        }
		}
	    
	%>

<br />
<jsp:include page="./next-fragment.jsp"></jsp:include>
<br />
<h4>Skriv inlägg</h4>
<form:form method="post" modelAttribute="post" id="postform"
	action="${postUrl}">



	<div>Namn:<form:input path="anonAuthor" size="5" /></div>

	<div><form:textarea path="content" rows="10" cols="60"></form:textarea>
	</div>
	<div><input type="submit" value="Skicka!" /> <font size="-1">
	Som svar på <form:input path="parentId" size="5" readonly="true"/>
	<form:hidden path="topParentId" /> </font></div>
</form:form>

<div id="footer">Render time: <%=new Date().getTime() - ticksStart %>
ms</div>

</div>



</div>
</center>

<script>

jQuery(function(){

    // expire in ten years!
    var expires_date = new Date( (new Date).getTime() + 1000 * 365 * 60 * 60 * 24 );
    document.cookie = 'lastVisit=' + new Date().getTime() + '; expires=' + expires_date.toGMTString() + '; path=/';
}
);

function setParentId ( parent, topparent )
{
        document.postform.parentId.value = parent;
        document.postform.topParentId.value = topparent;

}

function reply ( parent, topparent )
{
		jQuery('input[id="parentId"]').val(parent);
		jQuery('input[id="topParentId"]').val(topparent);
        

        jQuery("#anonAuthor").focus();

}

function setFlatMode( mode )
{
        // expire in ten years!
        var expires_date = new Date( (new Date).getTime() + 1000 * 365 * 60 * 60 * 24 );
        document.cookie = 'flatMode=' + mode + '; expires=' + expires_date.toGMTString() + '; path=/';
		document.getElementById("reloadForm").submit();
}

function showFull( id )
{
        document.getElementById('full_'+id).style.display = 'block'; 
        document.getElementById('mini_'+id).style.display = 'none';     
}

function showMini( id )
{
        document.getElementById('full_'+id).style.display = 'none'; 
        document.getElementById('mini_'+id).style.display = 'block';    
}
</script>